import { data, province, city, area, town } from 'province-city-china/data';
export function getAreaTree() {
  return province.map(item => {
    let cityList = city.filter(item2 => item2.province == item.province);
    if (!cityList.length) {
      return {
        value: item.code,
        label: item.name,
        children: [
          {
            value: item.code,
            label: item.name,
            children: area
              .filter(item2 => item2.province == item.province)
              .map(item3 => {
                return {
                  value: item3.code,
                  label: item3.name,
                };
              }),
          },
        ],
      };
    }
    return {
      value: item.code,
      label: item.name,
      children: cityList.map(item2 => {
        return {
          value: item2.code,
          label: item2.name,
          children: area
            .filter(
              item3 =>
                item3.city == item2.city && item3.province === item2.province
            )
            .map(item3 => {
              return {
                value: item3.code,
                label: item3.name,
              };
            }),
        };
      }),
    };
  });
}
export function getAreaCode(areaList: [string, string, string]) {
  let areaObj = getAreaTree();
  const province = areaObj.find(item => item.value === areaList[0]);
  const city = province.children.find(item => item.value === areaList[1]);
  const area = city.children.find(item => item.value === areaList[2]);
  return {
    provinceCode: province.value,
    provinceName: province.label,
    cityCode: city.value,
    cityName: city.label,
    areaCode: area.value,
    areaName: area.label,
  };
}
